package com.apexedu.portal.service.impl;

import com.apexedu.biz.Const;
import com.apexedu.biz.action.PortalAuthorizationAction;
import com.apexedu.biz.entity.TAccessAppReg;
import com.apexedu.framework.service.impl.AbstractBaseServiceSupport;
import com.apexedu.framework.util.DBUtil;
import com.apexedu.framework.util.Pager;
import com.apexedu.framework.util.SeqFactory;
import com.apexedu.identity.entity.TSysMenu;
import com.apexedu.identity.entity.TSysRole;
import com.apexedu.identity.entity.TSysRoleMenu;
import com.apexedu.portal.action.PortalServiceAction;
import com.apexedu.portal.entity.TYhzd;
import com.apexedu.util.StringUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by janabo on 2014-10-27.
 */
@Service("portalServiceService")
public class PortalServiceServiceImpl extends AbstractBaseServiceSupport {


    @Override
    public void saveOrUpdate(Object o) throws Exception {

    }

    @Override
    public void load(Object o) throws Exception {

    }

    @Override
    public  List<TAccessAppReg> list(Object myaction, Pager pager) throws Exception {
        PortalServiceAction action = (PortalServiceAction)myaction;
        //得到系统编号
        String appid = action.getParameter("appid");

        List<TAccessAppReg> list = queryAllMenu();
        if (StringUtil.isNotBlank(appid)) {
            int appno = 0;
            //根据appid得到对应的下标
            for (int i = 0; i < list.size(); i++) {
                if (list.get(i).getAppid().equals(appid)) {
                    appno = i;
                    break;
                }
            }
            action.setAppno(appno);
        }
        action.setParameter("appid", appid);
        action.setMenuList(list);
        return null;
    }

    /**
     * 查询所有的系统名称
     *
     * @return List<TAccessAppReg>
     */
    public List<TAccessAppReg> queryAllMenu() {
        String sql = "select appid ,appname from T_ACCESS_APP_REG where state=1 order by ordernum desc,appid";
        List<TAccessAppReg> list = DBUtil.queryAllBeanList(sql, TAccessAppReg.class);
        return list;
    }



    @Override
    public boolean remove(Object o) throws Exception {
        return false;
    }


    /**
     * 查询所有已添加的服务
     */
    public List<TSysRole> queryAllServiceType(String appid, String appno) {

        return null;
    }


    /**
     * 查询所有属于某个菜单下的资源
     */
    public List<TSysMenu> queryServiceMenu(String appid) {
        String sql = "select * from T_SYS_MENU where belongtoapp =? and (sftjdmh !='1' or sftjdmh is null) order by menuid";
        List<TSysMenu> list = DBUtil.queryAllBeanList(sql, TSysMenu.class, appid);
        return list;
    }



    /**
     * 添加系统到门户资源下
     */
    public String addPortal(String[] menus,String fwsyfl) throws Exception {
        List<TSysMenu> updates = new ArrayList<TSysMenu>();//修改是否添加到门户状态
        List<TSysMenu> adds = new ArrayList<TSysMenu>();//新增门户服务
            if(StringUtils.isNotBlank(fwsyfl)) {
                for (int j=0;j<menus.length;j++) {
                    String menuid = menus[j];
                    String myNewNodeId = getNewMenuIdByParentMenuId(fwsyfl);//新增服务id
                    TSysMenu menu = DBUtil.queryBean("select * from t_sys_menu where menuid=?", TSysMenu.class, menuid);
                    menu.setSftjdmh("1");
                    updates.add(menu);

                    String menupath = (String) DBUtil.queryFieldValue("select appurl from t_access_app_reg t where t.appid = ?", menu.getBelongtoapp());
                    menu.setMenupath(menupath + "/UIProcessor?Table=" + menu.getMenupath());
                    menu.setUpmenuid(fwsyfl);
                    menu.setBelongtoapp("13111414274386110001");
                    menu.setMenuid(myNewNodeId);
                    menu.setBizcode(menuid);
                    adds.add(menu);
                }
        }
        baseDao.saveAll(adds);
        baseDao.updateNotNullAll(updates);
        return Const.MESSAGE_SAVE_SUCCESS;
    }


    /**
     * 生成新的ID号 规则：根据父ID查询其下一级最大的ID号，然后增加一。
     *
     * @param parentid
     * @return 新的ID号
     * @throws Exception
     */
    private static String getNewMenuIdByParentMenuId(String parentid) throws Exception {
        return SeqFactory.getNewTreeIdByParentId("T_Sys_Menu", "menuid", "upMenuId", parentid);
    }


}
